﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using AutoShuntingSchedule.BLL.Sys;
using AutoShuntingSchedule.DAL.Models.Sys;
using AutoShuntingSchedule.UI.Common;
using AutoShuntingSchedule.UI.Forms;
using AutoShuntingSchedule.UI.Forms.Page;
using Sunny.UI;

namespace AutoShuntingSchedule.UI
{
    public partial class MainFormWebStyle : UIAsideHeaderMainFrame
    {
        private int menuInitWidth = 250;
        private int menuCloseWidth = 0;
        private bool menuOpen = true;
        private const int openSymbol = 361499;
        private const int closeSymbol = 361500;

        private SysUserLogic userLogic;
        private SysPermissionLogic permissionLogic;
        private List<UIPage> pageList = new List<UIPage>();

        public MainFormWebStyle()
        {
            InitializeComponent();
            userLogic = new SysUserLogic();
            permissionLogic = new SysPermissionLogic();
            var id = GlobalConfig.CurrentUser.Id;
            var list = userLogic.ContainsUser("admin", id) ? permissionLogic.GetList() : permissionLogic.GetList(id);
            //var list = permissionLogic.GetList();

            if (list.IsNullOrEmpty())
            {
                this.ShowInfoDialog("网络或服务器异常，请稍后重试", UIStyle.Inherited);
                return;
            }

            GlobalConfig.PermissionList = list;

            SetMenuState(true);

            //设置关联
            Aside.TabControl = MainTabControl;

            //增加页面到Main
            AddPage(new FPieChart(), 1000);
            AddPage(new RouteForm(), 1001);
            AddPage(new SysDataForm(), 1002);
            AddPage(new ScheduleGraphForm(), 1003);
            AddPage(new FSymbols(), 1004);
            AddPage(new FColorful(), 1005);

            int pageIndex = 1000;
            int symbolSize = 24;
            ////设置Header节点索引
            Aside.CreateNode("首页", 357357, symbolSize, 1000);
            Aside.CreateNode("系统配置", 61818, symbolSize, 1002);
            Aside.CreateNode("交路维护", 61451, symbolSize, 1001);
            Aside.CreateNode("调车图板", 61950, symbolSize, 1003);
            if (list == null || list.Count == 0)
            {
                TreeNode parent = Aside.CreateNode("系统管理", 61875, 24, 10000);
                List<Type> typeList = Assembly.GetExecutingAssembly().GetTypes().Where(it => it.IsClass).ToList();
                foreach (Type type in typeList)
                {
                    PageCodeAttribute attribute = type.GetCustomAttribute(typeof(PageCodeAttribute)) as PageCodeAttribute;
                    if (attribute == null)
                        continue;

                    MyPage page = (MyPage)type.CreateInstance();
                    page.Text = attribute.Encode;
                    page.SetAllPermission();
                    Aside.CreateChildNode(parent, AddPage(page, Guid.NewGuid()));
                }
            }
            else
            {
                //取出一级菜单
                List<SysPermission> root = list.Where(it => it.ParentId == "0").ToList();
                foreach (SysPermission permission in root)
                {
                    TreeNode parent = Aside.CreateNode(permission.Name, permission.SymbolIndex, 24, permission.SortCode.Value);
                    List<SysPermission> childList = list.Where(it => it.ParentId == permission.Id).ToList();
                    foreach (SysPermission child in childList)
                    {
                        UIPage page = CreateUIPage(child);
                        pageList.Add(page);
                        Aside.CreateChildNode(parent, AddPage(page, Guid.NewGuid()));
                    }
                }
            }


            Aside.CreateNode("字体图标", 362614, symbolSize, 1004);
            Aside.CreateNode("多彩主题", 558426, symbolSize, 1005);

            //显示默认界面
            Aside.SelectFirst();

            uiToolTip1.SetToolTip(uiAvatarAuth, $"{GlobalConfig.CurrentUser.DeptName}/{GlobalConfig.CurrentUser.RealName}", $"{GlobalConfig.CurrentUser.Account}", 61530, 32, UIColor.Green);
        }

        /// <summary>
        /// 根据不同的权限，创建不同的Page
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        private UIPage CreateUIPage(SysPermission permission)
        {
            //反射创建具体的页面
            List<Type> typeList = Assembly.GetExecutingAssembly().GetTypes().Where(it => it.IsClass).ToList();
            foreach (Type type in typeList)
            {
                PageCodeAttribute attribute = type.GetCustomAttribute(typeof(PageCodeAttribute)) as PageCodeAttribute;
                if (attribute == null)
                    continue;
                if (attribute.Encode != permission.EnCode)
                    continue;
                try
                {
                    MyPage page = (MyPage)type.CreateInstance();
                    page.Text = permission.Name;
                    page.Symbol = permission.SymbolIndex;
                    page.ButtonPermissionList = GlobalConfig.PermissionList.Where(it => it.ParentId == permission.Id).ToList();
                    return page;
                }
                catch
                {
                    MyPage page = new EmptyPage();
                    page.Text = permission.Name;
                    page.Symbol = permission.SymbolIndex;
                    return page;
                }
            }

            MyPage page1 = new EmptyPage();
            page1.Text = permission.Name;
            page1.Symbol = permission.SymbolIndex;
            return page1;
        }

        private void SetMenuState(bool state)
        {
            //this.uiSwitch1.Active = open;
            menuOpen = state;
            this.uiAvatarMenuOpen.Symbol = state ? openSymbol : closeSymbol;
            this.Aside.Width = state ? menuInitWidth : menuCloseWidth;
        }

        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            SetMenuState(value);
        }

        private void MainFormWebStyle_Shown(object sender, EventArgs e)
        {
            //AutoLoad();
            this.WindowState = FormWindowState.Maximized;
        }

        private void uiAvatar1_Click(object sender, EventArgs e)
        {
            SetMenuState(!menuOpen);
        }

        private void Aside_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {
            this.uiLabelMenuPath.Text = $"{node.FullPath}";
        }

        private void MainFormWebStyle_Load(object sender, EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }
        }
    }
}
